﻿using SD.Common;
using SD.Infrastructure.EntityBase;
using System;
using System.Text;
using SlamDunk.ValueObjects.Enums;

namespace SlamDunk.MES.Domain.Entities.ConfigurationContext
{
    /// <summary>
    /// 硬件
    /// </summary>
    public class Hardware : AggregateRootEntity
    {
        #region # 构造器

        #region 00.无参构造器
        /// <summary>
        /// 无参构造器
        /// </summary>
        protected Hardware() { }
        #endregion

        #region 01.创建硬件构造器
        /// <summary>
        /// 创建硬件构造器
        /// </summary>
        /// <param name="hardwareNo">硬件编号</param>
        /// <param name="hardwareName">硬件名称</param>
        /// <param name="hardwareType">硬件类型</param>
        /// <param name="model">型号</param>
        /// <param name="description">描述</param>
        public Hardware(string hardwareNo, string hardwareName, HardwareType hardwareType, string model, string description)
            : this()
        {
            base.Number = hardwareNo;
            base.Name = hardwareName;
            this.HardwareType = hardwareType;
            this.Model = model;
            this.Description = description;

            //初始化关键字
            this.InitKeywords();
        }
        #endregion

        #endregion

        #region # 属性

        #region 硬件类型 —— HardwareType HardwareType
        /// <summary>
        /// 硬件类型
        /// </summary>
        public HardwareType HardwareType { get; private set; }
        #endregion

        #region 型号 —— string Model
        /// <summary>
        /// 型号
        /// </summary>
        public string Model { get; private set; }
        #endregion

        #region 描述 —— string Description
        /// <summary>
        /// 描述
        /// </summary>
        public string Description { get; private set; }
        #endregion

        #region 导航属性 - 硬件连接 —— HardwareConnection Connection
        /// <summary>
        /// 导航属性 - 硬件连接
        /// </summary>
        public virtual HardwareConnection Connection { get; private set; }
        #endregion

        #endregion

        #region # 方法

        #region 修改硬件 —— void UpdateInfo(string hardwareNo, string hardwareName...
        /// <summary>
        /// 修改硬件
        /// </summary>
        /// <param name="hardwareNo">硬件编号</param>
        /// <param name="hardwareName">硬件名称</param>
        /// <param name="model">型号</param>
        /// <param name="description">描述</param>
        public void UpdateInfo(string hardwareNo, string hardwareName, string model, string description)
        {
            base.Number = hardwareNo;
            base.Name = hardwareName;
            this.Model = model;
            this.Description = description;

            //初始化关键字
            this.InitKeywords();
        }
        #endregion

        #region 配置网口通信 —— void ConfigureNetworkMode(Guid? terminalId, string ipAddress...
        /// <summary>
        /// 配置网口通信
        /// </summary>
        /// <param name="terminalId">终端Id</param>
        /// <param name="ipAddress">IP地址</param>
        /// <param name="port">端口号</param>
        public void ConfigureNetworkMode(Guid? terminalId, string ipAddress, int port)
        {
            if (this.Connection == null)
            {
                this.Connection = new HardwareConnection(terminalId, ipAddress, port);
            }
            else
            {
                this.Connection.ConfigureNetworkMode(terminalId, ipAddress, port);
            }
        }
        #endregion

        #region 配置串口通信 —— void ConfigureSerialPortMode(Guid? terminalId, string comPort...
        /// <summary>
        /// 配置串口通信
        /// </summary>
        /// <param name="terminalId">终端Id</param>
        /// <param name="comPort">串口端口号</param>
        /// <param name="baudRate">波特率</param>
        /// <param name="stopBit">停止位</param>
        /// <param name="parityBit">校验位</param>
        /// <param name="dataBit">数据位</param>
        /// <param name="dtr">数据终端准备</param>
        /// <param name="rts">请求发送</param>
        public void ConfigureSerialPortMode(Guid? terminalId, string comPort, int baudRate, StopBits stopBit, Parity parityBit, int dataBit, bool dtr, bool rts)
        {
            if (this.Connection == null)
            {
                this.Connection = new HardwareConnection(terminalId, comPort, baudRate, stopBit, parityBit, dataBit, dtr, rts);
            }
            else
            {
                this.Connection.ConfigureSerialPortMode(terminalId, comPort, baudRate, stopBit, parityBit, dataBit, dtr, rts);
            }
        }
        #endregion

        #region 配置USB口通信 —— void ConfigureUsbMode(Guid? terminalId)
        /// <summary>
        /// 配置USB口通信
        /// </summary>
        /// <param name="terminalId">终端Id</param>
        public void ConfigureUsbMode(Guid? terminalId)
        {
            if (this.Connection == null)
            {
                this.Connection = new HardwareConnection(terminalId);
            }
            else
            {
                this.Connection.ConfigureUsbMode(terminalId);
            }
        }
        #endregion

        #region 初始化关键字 —— void InitKeywords()
        /// <summary>
        /// 初始化关键字
        /// </summary>
        private void InitKeywords()
        {
            StringBuilder builder = new StringBuilder();
            builder.Append(base.Number);
            builder.Append(base.Name);
            builder.Append(this.HardwareType.GetEnumMember());
            builder.Append(this.Model);
            builder.Append(this.Description);

            base.SetKeywords(builder.ToString());
        }
        #endregion

        #endregion
    }
}
